/**
 * 向新邮箱发送验证码
 * mail    用户输入的新邮箱号码
 */

function ajaxGetCodeToNewMail(mail, that, code) {
    var url = "/Lvscore_User_M/account/ajaxGetCodeByNewMail";
    $.ajax({
        url:url,
        type: "post",
        //timeout:10000,
        data:{
            'mail': mail,
            'code': code
        },
        success:function(data) {
            /**
             * "0"：服务器异常 发送失败 "1"：发送成功 "-1"：该邮箱已被注册
             */
            that.disabled = false;
            if (data == '1') {
                alertBox("发送成功，请注意查收");
                settime(that);
            } else if (data == '-1') {
                setTimeout(function(){
                    validateSpanMsg("emailError","该邮箱已注册");
                    that.removeAttribute("disabled");
                    that.value = "获取验证码";
                    that.style.opacity = "1";
                },1000);
            } else if (data == '-2') {
                window.location.href = '/Lvscore_User_M/userWeb/views/accounterCenter/oldEmail.html';
            } else {
                setTimeout(function(){
                    alertBox("发送失败，请重试", '');
                    that.removeAttribute("disabled");
                    that.value = "获取验证码";
                    that.style.opacity = "1";
                },1000);
            }
        },
        error: function () {
            alertBox("网络异常，请重试","");
            //validateSpanMsg("codeError", "服务器异常，请重试");
            $("#get").val("重新发送");
        },
        dataType: "json"});
}

/**
 * 更新邮箱
 * mail    用户输入的新邮箱
 */
function ajaxSaveNewMail(mail) {
    var url = "/Lvscore_User_M/account/ajaxSaveNewMail";
    $.post(
        url,
        {'mail': mail},
        function (data) {
            if (data == 1) {
                window.location.href = "/Lvscore_User_M/userWeb/views/accounterCenter/changeEmailSucc.html"
            }
        },
        "json"
    );
}
/**
 * 验证新邮箱的验证码是否正确
 * code    用户输入的验证码
 */
function ajaxValidateCodeNewMail(code) {
    //loading("数据提交中...");
    var url = "/Lvscore_User_M/account/ajaxValidateCodeNewMail";
    $.ajax({
        url: url,
        type: "post",
        //timeout:10000,
        data: {
            'mail': $(".newEmail").val(),
            'code': code
        },
        success: function (data) {
            //$(".loadingBox").hide();
            if (data == 1) {
                ajaxSaveNewMail($(".newEmail").val());
            } else if(data == -1) {
                validateSpanMsg("codeError", "验证码错误");
            }else{
                alertBox("网络异常，请重试","");
                //validateSpanMsg("codeError", "服务器异常，请重试");
            }
        },
        error: function () {
            //$(".loadingBox").hide();
            alertBox("网络异常，请重试","");
            //validateSpanMsg("codeError", "服务器异常，请重试");
        },
        dataType: "json"
    });
}

$(function () {
    $(".newEmail").val("");
    $(".code").val("");
    getUrl('accounterCenter/accounterSecurity', 'accounterCenter/oldEmail');
    var code = getQueryString('code');
    if (!isNotEmpty(code)) {
        window.location.href = '/Lvscore_User_M/userWeb/views/accounterCenter/oldEmail.html';
    }
    //实时监控输入框
    inputChange(".newEmail","#emailError");
    inputChange(".code","#codeError");
    function emailMessage(){
        var emails = $(".newEmail").val();
        if (emails == "" || emails == null) {
            validateSpanMsg("emailError","请输入新邮箱");
            return false;
        }else if (!isEmail(emails)) {
            validateSpanMsg("emailError","邮箱格式不正确");
            return false;
        }else{
            return true;
        }
    }
    function codeMessage(){
        var code = $(".code").val();
        if(!isNotEmpty(code)){
            validateSpanMsg("codeError","请输入验证码");
            return false;
        }else if(!/^[A-Za-z0-9]{6}$/.test(code)){
            validateSpanMsg("codeError","验证码错误");
            return false;
        }else{
            return true;
        }
    }
    $(".newEmail").blur(function(){
        emailMessage();
    })
    $(".code").blur(function(){
        codeMessage();
    })
    $("#get").click(function () {
        var emails = $(".newEmail").val();
        if(emailMessage()){
            $(this).disabled = true;
            $(this).val("发送中...");
            ajaxGetCodeToNewMail(emails, this, code);
        }
    });
    //提交
    $("#sub").click(function () {
        var code = $(".code").val();
        if (emailMessage()&&codeMessage()) {
            ajaxValidateCodeNewMail(code);
        }
    });
});